package ServletMerchandise;

import Dao.McdDao;
import Entity.Merchandise;
import Entity.Merchant;
import Util.DateTool;
import Util.Page;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@WebServlet(name = "ServletMerchandise", value = "/merchandise")
public class ServletMerchandise extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String key = request.getParameter("key");
        String username = request.getParameter("username");
        if(key==null) key=" ";
        List<Merchandise> list = null;

        try {
            list = McdDao.queryByKey(key,username);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        request.setAttribute("list", list);
        request.setAttribute("username",username);

        //获取页码
        int pagenum = 1,total = list.size();
        pagenum = Integer.parseInt(request.getParameter("pagenum"));
        Page page = new Page(pagenum,total);
        request.setAttribute("page",page);
//        System.out.println("get:"+page);

        //获取商品配比
        Double sum = 0.0;
        List<Merchandise> Mcdlist = null;
        try {
            Mcdlist = McdDao.queryByKey("",request.getParameter("username"));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        for(Merchandise merchandise:Mcdlist){
            Double own = merchandise.getPrice()*merchandise.getNum();
            sum += own;
        }
        Collections.sort(Mcdlist, new Comparator<Merchandise>() {
            @Override
            public int compare(Merchandise o1, Merchandise o2) {
                double own1 = o1.getPrice()*o1.getNum();
                double own2 = o2.getPrice()*o2.getNum();
                if (own2>own1) return 1;
                else if(own2==own1) return 0;
                else return -1;
            }
        });
        if(Mcdlist.size()>5) request.setAttribute("Mcdlist",Mcdlist.subList(0,5));
        else request.setAttribute("Mcdlist",Mcdlist);
        request.setAttribute("sum",sum);

        request.getRequestDispatcher("merchandise.jsp").forward(request, response);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("name");
        double price = Double.parseDouble(request.getParameter("price"));
        int num = Integer.parseInt(request.getParameter("num"));
        int mctid = Integer.parseInt(request.getParameter("mctid"));
        String id = DateTool.getNowTime();
        String url = request.getParameter("url");
        Merchandise merchandise = new Merchandise(id, name, price, num, mctid, url);
        try {
            McdDao.save(merchandise);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        //刷新列表
        String username = request.getParameter("username");
        request.setAttribute("uername",username);
        List<Merchandise> list = null;
        try {
            list = McdDao.queryByKey(" ",username);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        request.setAttribute("list", list);

        //获取页码
        int pagenum = 1,total = list.size();
        pagenum = Integer.parseInt(request.getParameter("pagenum"));
        Page page = new Page(pagenum,total);
        request.setAttribute("page",page);
//        System.out.println("post:"+page);


        //获取商品配比
        Double sum = 0.0;
        List<Merchandise> Mcdlist = null;
        try {
            Mcdlist = McdDao.queryByKey("",request.getParameter("username"));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        for(Merchandise it:Mcdlist){
            Double own = it.getPrice()*it.getNum();
            sum += own;
        }
        Collections.sort(Mcdlist, new Comparator<Merchandise>() {
            @Override
            public int compare(Merchandise o1, Merchandise o2) {
                double own1 = o1.getPrice()*o1.getNum();
                double own2 = o2.getPrice()*o2.getNum();
                if (own2>own1) return 1;
                else if(own2==own1) return 0;
                else return -1;
            }
        });
        if(Mcdlist.size()>5) request.setAttribute("Mcdlist",Mcdlist.subList(0,5));
        else request.setAttribute("Mcdlist",Mcdlist);
        request.setAttribute("sum",sum);


        request.getRequestDispatcher("merchandise.jsp").forward(request, response);
    }
}
